//index.js
//获取应用实例
var util = require('../../utils/util.js');
var app = getApp();
var openId;  
var userId;
var wxprice;
var shareTitle;
Page({
    data: {
        i:0,
        userId:'',
        productId:'',
        buyFail:false,
        showshow:false,
        buycolor:false,
        loading: false,
        introduceList:[],
        introduce: true,
        contents: false,
        courseList: [],
        haveCinema: false,
        winHeight: "",//窗口高度
        currentTab: 0, //预设当前项的值
        scrollLeft: 0, //tab标题的滚动条位置
    },
   
    // setLoading
    setLoading:function(){
        this.setData({
            loading: !this.data.loading
        })
    },
    // 介绍
    swichNav1: function (e) {
        this.setData({
            currentTab: 0,
            introduce:true,
            contents: false
        })
        
    },
     // 目录
    swichNav2: function (e) {
        this.setData({
            currentTab: 1,
            introduce:false,
            contents:true
        })
            
    },
   
    wxPay:function(data){
        console.log(11111111)
    },
    buyfailconfirm:function(){
        this.setData({
            buyFail: false
        })
    },
    // 立即购买
    goBuy:function(){
        let that=this;
        that.setData({
            buycolor: true
        })
        openIds = wx.getStorageSync('openIds');
        productId = wx.getStorageSync('productId');
        if (wxprice==0){
            that.insertAppletOrder();
        }else{
            wx.request({
                url: app.globalData.url + "/getWxPay",
                data: {
                    openId: openIds,
                    // money: 0.01,
                    money: wxprice,
                    productId: productId

                },
                success: function (res) {
                    console.log(res.data.wx);
                    wx.requestPayment({
                        'appId': res.data.wx.appid,
                        'timeStamp': res.data.wx.timeStamp,
                        'nonceStr': res.data.wx.nonceStr,
                        'package': 'prepay_id=' + res.data.wx.prepayId,
                        'signType': 'MD5',
                        'paySign': res.data.wx.sign,
                        'success': function (res) {
                            console.log(res);
                            that.insertAppletOrder();
                        },
                        'fail': function (res) {
                            that.setData({
                                buyFail:true,
                                buycolor: false
                                
                            })
                        }
                    })
                }
            })
        }
        
    },
    insertAppletOrder:function(){
        let that=this;
        userId = wx.getStorageSync('userId');
        openId = wx.getStorageSync('openId');
        wx.request({
            url: app.globalData.url + "/insertAppletOrder",
            data: {
                openId: openId,
                userId: userId,
                productId: productId

            },
            success: function (res) {
                console.log(res);
                // if (res.code==-145){
                //     wx.showToast({
                //         title: '已购买请稍后重试',
                //         icon: 'none',
                //         duration: 2000
                //     })
                // }
                // wx.navigateTo({
                //     url: "/pages/courseDetail/courseDetail"
                // })
                that.getAppletCourse();
            }
        })
    },
    goPage:function(e){
        let that=this;
        console.log(e);
        wx.setStorageSync('indexI', e.currentTarget.dataset.mark)
        let url = e.currentTarget.dataset.url;
        let ispublic = e.currentTarget.dataset.ispublic;
        let isletpay = e.currentTarget.dataset.isletpay;
        console.log(e.currentTarget.dataset.isletpay)
        
        // 跳转
        if (ispublic == 1 || isletpay){
            wx.setStorageSync('qiehuan', 0) //切换页面相关
            wx.switchTab({
                url: url
            })
        }else{
            // 提示
            wx.showToast({
                title: '请先购买该课程',
                icon: 'none',
                duration: 2000
            })
        }
        
    },
    onLoad: function (option) {
        console.log(option)
        let that=this;
        // that.setData({
        //     productId: option.productId
        // })
        // productId = option.productId;
        wx.setStorageSync('productId', option.productId);
        wx.showLoading({
            title: '加载中',
            mask: true
        }); 
        
    },
    // 获取课程的详情
    getAppletCourse:function() {
        let that = this;
    
        openId = wx.getStorageSync('openId');
        productId = wx.getStorageSync('productId');
    
        wx.request({
            url: app.globalData.url + "/getAppletCourse",
            data: {
                openId: openId,
                productId: productId
                
            },
            success: function (res) {
                console.log(res);
                wx.setStorageSync('letCourse', res.data)//缓存某老师的课程介绍
                wxprice = res.data.course.price;
                shareTitle=res.data.course.name
                // 顶部导航设置
                // wx.setNavigationBarTitle({
                //     title: res.data.course.name
                // });
                if(res.data.course.isLetPay === false){
                    console.log(123)
                    that.setData({
                        introduceList: res.data.course,
                        showshow:true
                    })
                }else{
                    console.log(321)
                    that.setData({
                        introduceList: res.data.course,
                        showshow:false,
                        currentTab: 1,
                        introduce: false,
                        contents: true
                    })
                }
                wx.hideLoading();
               
            }
        })


    },
    // 获取课程的节数内容
    getAppletLessonList: function () {
        let that = this;
        openId = wx.getStorageSync('openId'); 
        userId = wx.getStorageSync('userId');
        productId = wx.getStorageSync('productId');
        
        wx.request({
            url: app.globalData.url + "/getAppletLessonList",
            data: {
                openId: openId,
                userId:userId,
                productId: productId

            },
            success: function (res) {
                console.log(res);
                wx.setStorageSync('letLessonList', res.data.Lesson)//缓存某老师的课
                let cl = res.data.Lesson.result;
                for (var i = 0, count = cl.length; i < count; i++) {
                    cl[i].duration = util.shijian(cl[i].duration) 
                }
                that.setData({
                    courseList: res.data.Lesson.result
                })
            }
        })
    },
    
    onShow: function () {
        // app.checkSettingStatu();
        this.getAppletCourse();
        this.getAppletLessonList(); 
    },
    // 分享功能
    onShareAppMessage: function (options) {
        let that = this;
        productId= wx.getStorageSync('productId')
        // 设置菜单中的转发按钮触发转发事件时的转发内容
        let shareObj = {
            title: shareTitle,        // 默认是小程序的名称(可以写slogan等)
            path: '/pages/courseDetail/courseDetail?productId='+productId,        // 默认是当前页面，必须是以‘/’开头的完整路径
            imgUrl: '',     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
            success: function (res) {
                // 转发成功之后的回调
                if (res.errMsg == 'shareAppMessage:ok') {
                    console.log(res);
                    wx.showShareMenu({
                        withShareTicket: true,
                        success: function () {
                            console.log(11111);
                        }
                    })
                }
            },
            fail: function (res) {
                // 转发失败之后的回调
                if (res.errMsg == 'shareAppMessage:fail cancel') {
                    // 用户取消转发
                } else if (res.errMsg == 'shareAppMessage:fail') {
                    // 转发失败，其中 detail message 为详细失败信息
                }
            },
            complete: function () {
                // 转发结束之后的回调（转发成不成功都会执行）
            }
        };
        // 来自页面内的按钮的转发
        if (options.from == 'button') {
            let eData = options.target.dataset;
            console.log(options);
            console.log(eData.name);     // shareBtn
            // 此处可以修改 shareObj 中的内容
            shareObj.path = '/pages/btnname/btnname?btn_name=' + eData.name;
        }
        // 返回shareObj
        return shareObj;
    }
})
